package utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

import utils.FileResources;

public class FileResources implements AbstractFile {
	static ArrayList<Object[]> sysDir;
	static ArrayList<Object[]> sysFile;
	
	/**
	 * List all files
	 */
	public static Vector<Object[]> listFiles(String pathName, boolean hasSmallIcon) {
		File f = new File(pathName);
		if(f != null){
			File files[] = f.listFiles();
			sysDir = new ArrayList<Object[]>();
			sysFile = new ArrayList<Object[]>();
			for(int i = 0; i < files.length; i++ ) {
				// each file has an extension , size and date
				Object[] item = new Object[4];
				//if it has icon get the icon
				if(hasSmallIcon){
					FileSystemView view = FileSystemView.getFileSystemView();
					Icon icon = view.getSystemIcon(files[i]);
				}
				item[0] = getName(files[i]);
				//set extension
				item[1] = getExtension(files[i]);
				//set size
				item[2] = getSize(files[i]);
				//set date
				item[3] = getDate(files[i]);
				
				if(files[i].isDirectory())
					sysDir.add(item);
				else
					sysFile.add(item);
			}

			Iterator<Object[]> iterDir = sysDir.iterator();
			Iterator<Object[]> iterFile = sysFile.iterator();

			Vector<Object[]>  v = new Vector<Object[]>();

			while(iterDir.hasNext()) {
				v.add(iterDir.next());
			}

			while(iterFile.hasNext()) {
				v.add(iterFile.next());
			}

			return v;
		}
		return null; 
	}
	
	/**
	 * Get filename
	 */
    public static String getName(File f) {
        String name = f.getName();
        if(!f.isDirectory())
        {
            int e = name.lastIndexOf(".");
            if(e > 0)
                name = name.substring(0,e);
        }
        return name;
    }
	
	/**
	 * Get file extension
	 */
	public static String getExtension(File f) {
        String extension = "";
        String name = f.getName();
        if(!f.isDirectory())
        {
            int e = name.lastIndexOf(".");
            if(e > 0)
                extension = name.substring(e + 1, name.length());
        }
        return extension;
    }

	/**
	 * Get file size
	 */
	public static String getSize(File f) {
		String rs = "<DIR>";
		if(!f.isDirectory())
			rs = "" + f.length();
		return rs;
	}

	/**
	 * Get last modified date
	 */
	public static String getDate(File f) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		return sdf.format(f.lastModified());
	}

	public void acceptVisitor(DeleteVisitor v) {
		v.visit(this,null);
	}

	public void acceptVisitor(CopyVisitor v) {
		v.visit(this, null, null);
	}

	public void acceptVisitor(MoveVisitor v) {
		v.visit(this, null, null);
	}

	public void acceptVisitor(NewFolderVisitor v) {
		v.visit(this, null);
	}

	public void acceptVisitor(RenameVisitor v) {
		v.visit(this, null, null);
	}

}
